package com.lin.fengxiu.service;

import com.lin.fengxiu.model.Spu;
import com.lin.fengxiu.repository.SpuRepository;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName SpuService
 * @Description TODO
 * @Author wushaopei
 * @Date 2021/3/13 14:08
 * @Version 1.0
 */
@Service
public class SpuService {

    @Autowired
    private SpuRepository spuRepository;

    public Spu getSpu(Long id){
        return spuRepository.findOneById(id);
    }

    public Page<Spu> getLatestPagingSpu(Integer pageNum , Integer size){
        Pageable page = PageRequest.of(pageNum, size, Sort.by("createTime").descending());
        return this.spuRepository.findAll(page);
    }

    public Page<Spu> getByCategory(Long cid, Boolean isRoot, Integer pageNum, Integer size){
        Pageable page = PageRequest.of(pageNum,size);
        if(isRoot){
            return this.spuRepository.findByRootCategoryIdOrderByCreateTime(cid, page);
        }else{
            return this.spuRepository.findByCategoryIdOrderByCreateTimeDesc(cid, page);
        }
    }
}
